import React, {Component} from 'react';
import {Button, Checkbox, Form} from 'antd';
import moment from 'moment';
import 'moment/locale/zh-cn';
import {ModalFooter} from 'antd-extra';
import notify from '../../NotificationExtra';
import SiderTree from '../SiderTree';

const _ = require('lodash');
moment.locale('zh-cn');

@Form.create()
//@connect(({ workOrder, global }) => ({ ...workOrder, ...global }))
class ModalMoveNode extends Component {
  constructor(props) {
    super(props);
    this.state = {
      checkedKeys: [],
    };
  }

  componentDidMount() {

  }

  close = () => {
    const { result, treeId, fatherId, form } = this.props;
    const { checkedKeys } = this.state;
    if (!checkedKeys || !checkedKeys.length) {
      notify({ message: '未选择目录', type: 'error' });
      return;
    }
    const distId = checkedKeys[checkedKeys.length - 1];

    if (distId === treeId) {
      notify({ message: '不能移动到自己', type: 'error' });
      return;
    }
    if (distId === fatherId) {
      notify({ message: '已在该目录下', type: 'error' });
      return;
    }
    const data = form.getFieldsValue();
    const { isCopy } = data;
    result && result({ distId, isCopy });
    this.props.modalRef.close();
  };

  onCheck = key => {
    this.setState({ checkedKeys: [key] });
  };

  render() {
    const { form: { getFieldDecorator }, disableCopy, table, dispatch } = this.props;

    const _props = { dispatch, checkable: true, table, onCheck: this.onCheck };
    return (
      <Form>
        {<SiderTree
          {..._props}
        ></SiderTree>}
        {
          !disableCopy && getFieldDecorator('isCopy', { initialValue: false })
          (<Checkbox style={{ marginTop: 20, float: 'right' }}>复制</Checkbox>)
        }
        <ModalFooter>
          <Button type="primary"
                  onClick={() => {
                    this.close();
                  }}
          >确认
          </Button>
          <Button onClick={() => {
            this.props.modalRef.close();
          }}
          >取消
          </Button>
        </ModalFooter>
      </Form>
    );
  }
}

export default ModalMoveNode;
